package juc.createThread;

import java.util.concurrent.*;

public class Test {
    @org.junit.Test
    public void test() throws InterruptedException {
        MyThread thread = new MyThread();
        thread.start();
//        TimeUnit.SECONDS.sleep(2);
//        Thread.sleep(3);
        //如下操作仍然是在main线程中执行的。
        for (int i = 0; i < 100; i++) {
            if (i % 2 == 0) {
                System.out.println(Thread.currentThread().getName() + ":" + i + "***********main()************");
            }
        }

//        new Thread(){
//            @Override
//            public void run() {
//                super.run();
//            }
//        }.start();


    }

    @org.junit.Test
    public void test2(){
        //3. 创建实现类的对象
        MyThread2 mThread = new MyThread2();
        //4. 将此对象作为参数传递到Thread类的构造器中，创建Thread类的对象
        Thread t1 = new Thread(mThread);
        t1.setName("线程1");
        //5. 通过Thread类的对象调用start():① 启动线程 ②调用当前线程的run()-->调用了Runnable类型的target的run()
        t1.start();

        //再启动一个线程，遍历100以内的偶数
        Thread t2 = new Thread(mThread);
        t2.setName("线程2");
        t2.start();


        new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    if (i % 2 == 0) {
                        System.out.println(Thread.currentThread().getName() + ":" + i + "***********main()************");
                    }
                }
            }
        };

//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//
//            }
//        }).start();
    }


    @org.junit.Test
    public void test3() {
        FutureTask<String> futureTask = new FutureTask<>(new MyThread3());
        new Thread(futureTask).start();
        try {
            System.out.println(futureTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

//        new Thread(new FutureTask<String>(
//                new Callable<String>() {
//                    @Override
//                    public String call() throws Exception {
//                        return null;
//                    }
//                }
//        ){
//        }).start();
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread();
        t1.start();
        t1.join();

        System.out.println("t1线程结束之后，main线程才接着执行执行");
    }
}
